package com.cqc.retrofitdemo02;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;

import com.cqc.retrofitdemo02.event.AppEvent;
import com.cqc.retrofitdemo02.event.HttpErrorEvent;
import com.cqc.retrofitdemo02.event.HttpSuccessEvent;
import com.cqc.retrofitdemo02.http.HttpRequest;

import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;
import retrofit2.converter.scalars.ScalarsConverterFactory;

public class MainActivity extends BaseActivity implements View.OnClickListener {

    private static final String TAG = "MainActivity";
    private Button btn1;
    private Button btn2;
    private Button btn3;
    private Button btn4;
    private Button btn5;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        findViews();
        initViews();
    }

    @Override
    public void appEvent(AppEvent event) {

    }

    @Override
    public void httpErrorEvent(HttpErrorEvent event) {

    }

    @Override
    public void httpSuccessEvent(HttpSuccessEvent event) {

    }


    private void findViews() {
        btn1 = (Button) findViewById(R.id.btn1);
        btn2 = (Button) findViewById(R.id.btn2);
        btn3 = (Button) findViewById(R.id.btn3);
        btn4 = (Button) findViewById(R.id.btn4);
        btn5 = (Button) findViewById(R.id.btn5);
    }

    private void initViews() {
        btn1.setOnClickListener(this);
        btn2.setOnClickListener(this);
        btn3.setOnClickListener(this);
        btn4.setOnClickListener(this);
        btn5.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.btn1:
                post1();
                break;
            case R.id.btn2:
                post2();
                break;
            case R.id.btn3:
                post3();
                break;
            case R.id.btn4:
                post4();
                break;
            case R.id.btn5:

                break;
        }
    }

    private void post4() {
        String key = "47bf98583704a0f19d1d8a258c78d909";
        int areaid = 110000;
        new HttpRequest().post4(key,areaid);
    }

    private void post3() {
        //1 创建retrofit对象
        String baseUrl = "http://192.168.56.1:8080/QQ/";
        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl(baseUrl)
                .addConverterFactory(GsonConverterFactory.create())
                .build();
        //2 获取接口
        AppRequest appRequest = retrofit.create(AppRequest.class);
        //3 调用接口的方法
        Call<List<InfoBean>> call = appRequest.post3();
        //4
        call.enqueue(new Callback<List<InfoBean>>() {
            @Override
            public void onResponse(Call<List<InfoBean>> call, Response<List<InfoBean>> response) {
                Log.d(TAG, "onResponse=" + response.body().get(0).getCommenturl());
            }

            @Override
            public void onFailure(Call<List<InfoBean>> call, Throwable t) {
                Log.d(TAG, "onFailure=" + t.getMessage() + "\n t=" + t.toString());
            }
        });
    }

    private void post2() {
        //1 创建retrofit对象
        String baseUrl = "http://192.168.56.1:8080/QQ/";
        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl(baseUrl)
                .addConverterFactory(ScalarsConverterFactory.create())
                .build();
        //2 获取接口
        AppRequest appRequest = retrofit.create(AppRequest.class);
        //3 调用接口的方法
        Call<String> call = appRequest.post2();
        //4 获取网络数据
        call.enqueue(new Callback<String>() {
            @Override
            public void onResponse(Call<String> call, Response<String> response) {
                Log.d(TAG, "onResponse=" + response.body());
            }

            @Override
            public void onFailure(Call<String> call, Throwable t) {
                Log.d(TAG, "onFailure=" + t.getMessage() + "\n t=" + t.toString());
            }
        });
    }

    private void post1() {
        String key = "47bf98583704a0f19d1d8a258c78d909";
        //baseUrl必须以/结尾
        String baseUrl = "http://v.juhe.cn/xiangji_weather/";
        //1 创建retrofit对象
        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl(baseUrl)
                .addConverterFactory(ScalarsConverterFactory.create())
                .build();
        //2 获取我们已经创建的接口AppUrl
        AppRequest appRequest = retrofit.create(AppRequest.class);
        //3 调用我们创建的方法，获取call
        Call<String> call = appRequest.post1(key, 110000);
        //4 开始连接网络
        call.enqueue(new Callback<String>() {
            @Override
            public void onResponse(Call<String> call, Response<String> response) {
                Log.d(TAG, "onResponse=" + response.body());
            }

            @Override
            public void onFailure(Call<String> call, Throwable t) {
                Log.d(TAG, "onFailure=" + t.getMessage() + "\n t=" + t.toString());
            }
        });
    }
}
